package com.sghl.common.utils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Random;

public class RandomStringGenerator {

    /**
     * 获取一定长度的随机字符串
     *
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";
//        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取一定长度的随机字符串
     *
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomIntegerByLength(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获取文件名称
     *
     * @return 一定长度的字符串
     */
    public static String getRandomFileName() {
        return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() + RandomStringGenerator.getRandomStringByLength(8);
    }

    /**
     * 获取一定长度的随机字符串
     *
     * @return 一定长度的字符串
     */
    public static String getRandomSuffixName(String name) {
        return getRandomSuffixName(name, 7);
    }

    /**
     * 获取一定长度的随机字符串
     *
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomSuffixName(String name, int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        if (name.contains("#")) {
            name = name.split("#")[0];
        }
        return name + "#" + sb.toString();
    }

}